package com.andryu.video

import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import android.view.WindowManager
import android.window.OnBackInvokedDispatcher
import androidx.activity.OnBackPressedCallback
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.andryu.base.constant.CommonConstants
import com.andryu.base.ui.BaseActivity
import com.andryu.video.databinding.ActivityTutorialVideoBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 音视频教程activity界面
 */
@AndroidEntryPoint
class TutorialVideoActivity : BaseActivity() {

    private lateinit var binding: ActivityTutorialVideoBinding

    @NaviVideoType.NaviVideoType1
    private var mType: Int = NaviVideoType.NAVI_VIDEO_JNI

    private lateinit var navController: NavController
    private val destinationChangedListener =
        NavController.OnDestinationChangedListener { _, destination, _ ->

        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        addStatusBarColor()
        binding = ActivityTutorialVideoBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
        initObserver()

    }

    /**
     * 初始化view
     */
    private fun initView() {
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
        mType = intent.getIntExtra(CommonConstants.NAVI_INTENT_INT, NaviVideoType.NAVI_VIDEO_JNI)
        val jumpId = intent.getIntExtra(CommonConstants.NAVI_FRAGMENT_JUMP_INT, -1)
        when (mType) {
            NaviVideoType.NAVI_VIDEO_JNI -> navController.navigate(R.id.chapter_simple_ndk)
            NaviVideoType.NAVI_VIDEO_FFMPEG -> navController.navigate(R.id.chapter_ffmpeg)
            NaviVideoType.NAVI_VIDEO_GRIFIKA -> navController.navigate(R.id.chapter_grifika)
            NaviVideoType.NAVI_VIDEO_OPENGL -> {
                val uri = Uri.parse("learn://chapter_opengl")
                navController.navigate(uri)
            }
        }

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initObserver(){
        // 添加监听器以跟踪导航变化
        navController.addOnDestinationChangedListener(destinationChangedListener)

        // 物理按键返回
        onBackPressed {
            onHandleBackPressed()
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                onHandleBackPressed()
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    /**
     * 处理头部返回按钮
     */
    private fun onHandleBackPressed(): Boolean {
        return if (navController.currentDestination?.id == com.andryu.opengl.R.id.chapter_opengl || navController.currentDestination?.id == navController.graph.startDestinationId
            || navController.currentDestination?.id == R.id.chapter_ffmpeg || navController.currentDestination?.id == R.id.chapter_grifika
        ) {
            // 如果当前是目标 Fragment，则退出应用
            finish()
            true
        } else {
            // 否则，让 NavController 处理返回操作
            navController.navigateUp() || super.onSupportNavigateUp()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        navController.removeOnDestinationChangedListener(destinationChangedListener)
    }

}